<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" :model="page.query" inline label-position="right" label-width="90px">
        <el-form-item label="产品名称" prop="productId">
          <product-query ref="productQuery" v-model="page.query.productId" @choose="handleChooseProduct" />
        </el-form-item>
        <el-form-item label="库存编号" prop="stockNo">
          <el-input v-model="page.query.stockNo" placeholder="请输入库存编号" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <!--      <div>
        <span>库存信息列表</span>
      </div>
      <el-divider />-->
      <div class="table-button">
        <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" />
        <el-table-column label="产品名称" prop="productName" min-width="160" />
        <el-table-column label="变动类型" prop="type" min-width="80">
          <template slot-scope="scope">
            <stock-operation-type-tag v-model="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="变动数量" prop="quantity" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.quantity }}{{ scope.row.unit }}
          </template>
        </el-table-column>
        <el-table-column label="变动前库存数量" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.beforeStock }}{{ scope.row.unit }}
          </template>
        </el-table-column>
        <el-table-column label="变动后库存数量" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.afterStock }}{{ scope.row.unit }}
          </template>
        </el-table-column>
        <el-table-column label="变动时间" prop="createTime" min-width="150" />
        <el-table-column label="备注" prop="remark" min-width="150" />
        <el-table-column label="操作" min-width="80" fixed="right">
          <template slot-scope="scope">
            <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              placement="top"
              style="margin-left: 10px"
              title="是否删除此条数据"
              @confirm="handleDelConfirm(scope.row)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import { deleteStockLog, selectStockLogPage } from '@/api/erp/stockLog'

export default {
  name: 'Index',
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          stockNo: '',
          productName: '',
          categoryId: ''
        }
      }
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      selectStockLogPage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.page.query.productId = undefined
      this.$refs.productQuery.resetQueryForm()
      this.doPage()
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    handleChooseProduct(row) {
      if (row) {
        this.page.query.productId = row.id
      }
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      deleteStockLog(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 批量删除点击
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据,是否继续?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        deleteStockLog(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.tableLoading = false
          this.doPage()
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
